package com.example.finaltool.service;

import com.example.finaltool.annotations.Timer;
import com.example.finaltool.mapper.*;
import com.example.finaltool.model.Result;
import com.example.finaltool.utils.PrintUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dengbw
 * @date 2021/8/23 3:49 下午
 */
@Service
public class ToolService {

    SysCorpMapper sysCorpMapper;
    SysCorpVillageMapper sysCorpVillageMapper;
    CmmFunctionExtConfigMapper cmmFunctionExtConfigMapper;
    CmmFunctionConfigMapper cmmFunctionConfigMapper;
    SysConfigMapper sysConfigMapper;
    SysCorpStationMapper sysCorpStationMapper;
    SysLogMapper sysLogMapper;
    MenuMapper menuMapper;

    @Autowired
    public ToolService(SysCorpMapper sysCorpMapper,
                       SysCorpVillageMapper sysCorpVillageMapper,
                       CmmFunctionExtConfigMapper cmmFunctionExtConfigMapper,
                       CmmFunctionConfigMapper cmmFunctionConfigMapper,
                       SysConfigMapper sysConfigMapper,
                       SysCorpStationMapper sysCorpStationMapper,
                       SysLogMapper sysLogMapper,
                       MenuMapper menuMapper) {
        this.sysCorpMapper = sysCorpMapper;
        this.sysCorpVillageMapper = sysCorpVillageMapper;
        this.cmmFunctionExtConfigMapper = cmmFunctionExtConfigMapper;
        this.cmmFunctionConfigMapper = cmmFunctionConfigMapper;
        this.sysConfigMapper = sysConfigMapper;
        this.sysCorpStationMapper = sysCorpStationMapper;
        this.sysLogMapper = sysLogMapper;
        this.menuMapper = menuMapper;
    }

    @Timer("清除数据")
    public Result<Boolean> clean(String corpId) {
        PrintUtils.separator();
        LocalDateTime start = LocalDateTime.now();
        if (StringUtils.isBlank(corpId)) {
            return Result.success();
        }
        Map<String, Object> map1 = new HashMap<>(1);
        map1.put("tenant_id", corpId);
        Map<String, Object> map2 = new HashMap<>(1);
        map2.put("ding_corp_id", corpId);
        sysCorpMapper.deleteByMap(map1);
        sysCorpVillageMapper.deleteByMap(map1);
        cmmFunctionExtConfigMapper.deleteByMap(map1);
        cmmFunctionConfigMapper.deleteByMap(map1);
        sysConfigMapper.deleteByMap(map1);
        sysCorpStationMapper.deleteByMap(map2);
        sysLogMapper.delete(corpId);
        menuMapper.deleteMenu(corpId);
        menuMapper.deleteRole(corpId);
        menuMapper.deleteRoleMenu(corpId);
        menuMapper.deleteOrg(corpId);
        return Result.success();
    }

    public Boolean cleanCun(Long subid) {
        Map<String, Object> option = Collections.singletonMap("sub_tenant_id", subid);
        sysCorpMapper.deleteByMap(option);
        sysCorpVillageMapper.deleteByMap(option);
        cmmFunctionExtConfigMapper.deleteByMap(option);
        cmmFunctionConfigMapper.deleteByMap(option);
        sysConfigMapper.deleteByMap(option);
        sysCorpStationMapper.deleteByMap(option);
        sysLogMapper.deleteCun(subid);
        menuMapper.deleteCunMenu(subid);
        menuMapper.deleteCunRole(subid);
        menuMapper.deleteCunRoleMenu(subid);
        menuMapper.deleteCunOrg(subid);
        return true;
    }
}
